package com.jiayou


import android.content.Context
import android.widget.Toast
import cn.hutool.json.JSONUtil
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUIDialog.MessageDialogBuilder
import kotlinx.android.synthetic.main.activity_main.*
import org.java_websocket.client.WebSocketClient
import org.java_websocket.handshake.ServerHandshake
import java.net.URI


class Client(var context: Context, uri: URI) : WebSocketClient(uri) {
    private val mainActivity = context as GameActivity
    var flag = 0
    override fun onOpen(serverHandshake: ServerHandshake) {
        mainActivity.runOnUiThread {
            Toast.makeText(mainActivity, "服务器连接成功！！", Toast.LENGTH_SHORT).show()
        }
        println("连接完成！")
    }

    override fun onMessage(s: String) {


        mainActivity.playMusic()

        //把对局信息更新
        mainActivity.messageChannel =
            JSONUtil.toBean(JSONUtil.toJsonPrettyStr(s), MessageChannel::class.java)

        val channel = JSONUtil.toBean(JSONUtil.toJsonPrettyStr(s), MessageChannel::class.java)
        println(JSONUtil.toBean(JSONUtil.toJsonPrettyStr(s), MessageChannel::class.java))

        //玩家异常退出
        if (channel.type == MessageState.QUIT) {
            mainActivity.go = ""
            mainActivity.messageChannel = null
            mainActivity.flag = 0
            mainActivity.runOnUiThread {
                mainActivity.victort.start()
                if (mainActivity.GAMEMODE == 1)
                    MessageDialogBuilder(mainActivity)
                        .setTitle("对局异常")
                        .setMessage("对方逃♂跑啦 (*^▽^*)！！")
                        .addAction(
                            "了♂解"
                        ) { dialog, _ -> dialog.dismiss() }
                        .show()
                else
                    MessageDialogBuilder(mainActivity)
                        .setTitle("对局异常")
                        .setMessage("对方逃跑啦！！")
                        .addAction(
                            "了解"
                        ) { dialog, _ -> dialog.dismiss() }
                        .show()
            }
            return
        }


        //玩家标识
        if (channel.data?.player1?.equals(mainActivity.user)!!)
            mainActivity.flag = 1
        else
            mainActivity.flag = 2

        //第一次接收到消息
        if (flag++ == 0)
            mainActivity.runOnUiThread {
                mainActivity.tip.dismiss()
                mainActivity.runOnUiThread {
                    Toast.makeText(context, "匹配成功!", Toast.LENGTH_SHORT).show()
                    if (mainActivity.messageChannel?.data?.go.equals(mainActivity.user))
                        Toast.makeText(context, "这局由你先手!", Toast.LENGTH_LONG).show()
                    else
                        Toast.makeText(context, "等待对方玩家先手!", Toast.LENGTH_LONG).show()
                }
                if (mainActivity.messageChannel != null)
                    mainActivity.match.isClickable = false
            }


        mainActivity.go = channel.data?.go.toString()

        //渲染棋盘
        channel.data?.data?.forEachIndexed { index1, ints ->
            (context as GameActivity).runOnUiThread {
                when (index1) {
                    0 -> {
                        ints.forEachIndexed { index2, i ->
                            when (index2) {
                                0 -> {
                                    if (mainActivity.GAMEMODE == 1) {
                                        if (i == 1)
                                            mainActivity.l1.setBackgroundResource(R.drawable.player1)
                                        if (i == 2)
                                            mainActivity.l1.setBackgroundResource(R.drawable.player2)
                                    } else {
                                        if (i == 1)
                                            mainActivity.l1.setBackgroundResource(R.drawable.player11)
                                        if (i == 2)
                                            mainActivity.l1.setBackgroundResource(R.drawable.player22)
                                    }
                                }
                                1 -> {
                                    if (mainActivity.GAMEMODE == 1) {
                                        if (i == 1)
                                            mainActivity.l2.setBackgroundResource(R.drawable.player1)
                                        if (i == 2)
                                            mainActivity.l2.setBackgroundResource(R.drawable.player2)
                                    } else {
                                        if (i == 1)
                                            mainActivity.l2.setBackgroundResource(R.drawable.player11)
                                        if (i == 2)
                                            mainActivity.l2.setBackgroundResource(R.drawable.player22)
                                    }
                                }
                                2 -> {
                                    if (mainActivity.GAMEMODE == 1) {
                                        if (i == 1)
                                            mainActivity.l3.setBackgroundResource(R.drawable.player1)
                                        if (i == 2)
                                            mainActivity.l3.setBackgroundResource(R.drawable.player2)
                                    } else {
                                        if (i == 1)
                                            mainActivity.l3.setBackgroundResource(R.drawable.player11)
                                        if (i == 2)
                                            mainActivity.l3.setBackgroundResource(R.drawable.player22)
                                    }
                                }
                            }
                        }
                    }
                    1 -> {
                        ints.forEachIndexed { index2, i ->
                            when (index2) {
                                0 -> {
                                    if (mainActivity.GAMEMODE == 1) {
                                        if (i == 1)
                                            mainActivity.l4.setBackgroundResource(R.drawable.player1)
                                        if (i == 2)
                                            mainActivity.l4.setBackgroundResource(R.drawable.player2)
                                    } else {
                                        if (i == 1)
                                            mainActivity.l4.setBackgroundResource(R.drawable.player11)
                                        if (i == 2)
                                            mainActivity.l4.setBackgroundResource(R.drawable.player22)
                                    }
                                }
                                1 -> {
                                    if (mainActivity.GAMEMODE == 1) {
                                        if (i == 1)
                                            mainActivity.l5.setBackgroundResource(R.drawable.player1)
                                        if (i == 2)
                                            mainActivity.l5.setBackgroundResource(R.drawable.player2)
                                    } else {
                                        if (i == 1)
                                            mainActivity.l5.setBackgroundResource(R.drawable.player11)
                                        if (i == 2)
                                            mainActivity.l5.setBackgroundResource(R.drawable.player22)
                                    }
                                }
                                2 -> {
                                    if (mainActivity.GAMEMODE == 1) {
                                        if (i == 1)
                                            mainActivity.l6.setBackgroundResource(R.drawable.player1)
                                        if (i == 2)
                                            mainActivity.l6.setBackgroundResource(R.drawable.player2)
                                    } else {
                                        if (i == 1)
                                            mainActivity.l6.setBackgroundResource(R.drawable.player11)
                                        if (i == 2)
                                            mainActivity.l6.setBackgroundResource(R.drawable.player22)
                                    }
                                }
                            }
                        }
                    }
                    2 -> {
                        ints.forEachIndexed { index2, i ->
                            when (index2) {
                                0 -> {
                                    if (mainActivity.GAMEMODE == 1) {
                                        if (i == 1)
                                            mainActivity.l7.setBackgroundResource(R.drawable.player1)
                                        if (i == 2)
                                            mainActivity.l7.setBackgroundResource(R.drawable.player2)
                                    } else {
                                        if (i == 1)
                                            mainActivity.l7.setBackgroundResource(R.drawable.player11)
                                        if (i == 2)
                                            mainActivity.l7.setBackgroundResource(R.drawable.player22)
                                    }
                                }
                                1 -> {
                                    if (mainActivity.GAMEMODE == 1) {
                                        if (i == 1)
                                            mainActivity.l8.setBackgroundResource(R.drawable.player1)
                                        if (i == 2)
                                            mainActivity.l8.setBackgroundResource(R.drawable.player2)
                                    } else {
                                        if (i == 1)
                                            mainActivity.l8.setBackgroundResource(R.drawable.player11)
                                        if (i == 2)
                                            mainActivity.l8.setBackgroundResource(R.drawable.player22)
                                    }
                                }
                                2 -> {
                                    if (mainActivity.GAMEMODE == 1) {
                                        if (i == 1)
                                            mainActivity.l9.setBackgroundResource(R.drawable.player1)
                                        if (i == 2)
                                            mainActivity.l9.setBackgroundResource(R.drawable.player2)
                                    } else {
                                        if (i == 1)
                                            mainActivity.l9.setBackgroundResource(R.drawable.player11)
                                        if (i == 2)
                                            mainActivity.l9.setBackgroundResource(R.drawable.player22)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


        //判断对局是否已经结束
        if (channel.type == MessageState.GAMEOVER) {
            mainActivity.go = ""
            mainActivity.messageChannel = null
            mainActivity.flag = 0
            mainActivity.runOnUiThread {
                println(channel.data?.winner)
                println(mainActivity.user)
                if (channel.data?.winner.equals(mainActivity.user)) {
                    mainActivity.victort.start()
                    MessageDialogBuilder(mainActivity)
                        .setTitle("对局结束")
                        .setMessage("恭喜你赢得了这局游戏!")
                        .addAction(
                            "在玩一把"
                        ) { dialog, _ -> dialog.dismiss() }
                        .show()
                } else {
                    mainActivity.fail.start()
                    MessageDialogBuilder(mainActivity)
                        .setTitle("对局结束")
                        .setMessage("跟遗憾，对面比你更高一筹！")
                        .addAction(
                            "继续玩"
                        ) { dialog, _ -> dialog.dismiss() }
                        .show()
                }

            }
            return
        }


        //判断是否平局
        if (channel.type == MessageState.DRAW) {
            mainActivity.go = ""
            mainActivity.messageChannel = null
            mainActivity.flag = 0
            mainActivity.runOnUiThread {
                mainActivity.showDrawDiglog()
            }
            return
        }

        println("收到消息$s")
    }


    override fun onClose(i: Int, s: String, b: Boolean) {
        println(s)
        println(b)
        println("连接关闭！")
    }

    override fun onError(e: Exception) {
        println("连接错误！")
        throw  e
    }
}